//
//  OTCregisterViewController.m
//  gpos_app
//
//  Created by apple on 2018/10/12.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "OTCregisterViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "EBCountDownButton.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"
#import "XMRegisterViewController.h"
#import "XMLightningViewController.h"

@interface OTCregisterViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField * phoneText;
@property (nonatomic,strong)UITextField * possWordText;
@property (nonatomic,strong)UIButton * registerButton;
@property (nonatomic,strong)WKProgressHUD *hud;

@end

@implementation OTCregisterViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];;
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.title = @"转入闪电网络";
    [self UIloadView];
}

- (void)UIloadView{
    UIImageView * sjImage = [[UIImageView alloc] init];
    [sjImage setImage:[UIImage imageNamed:@"icon_shouji"]];
    [self.view addSubview:sjImage];
    
    [sjImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.top.equalTo(self.view).with.offset(kNavbarHeight+60);
        
    }];
    
    self.phoneText = [[UITextField alloc] init];
    self.phoneText.placeholder =@"请输入闪电网络账号";
    self.phoneText.keyboardType=UIKeyboardTypeNumberPad;
    self.phoneText.font = [UIFont systemFontOfSize:14.f];
    //    self.phoneText.secureTextEntry = YES;
    self.phoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneText.delegate = self;
    [self.view addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sjImage).with.offset(22);
        make.centerY.equalTo(sjImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    UIView * phoneView = [[UIView alloc] init];
    phoneView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(sjImage.mas_bottom).with.offset(10);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIView * yzmView = [[UIView alloc] init];
    yzmView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:yzmView];
    [yzmView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(phoneView.mas_bottom).with.offset(60);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIImageView * mimaImage = [[UIImageView alloc] init];
    [mimaImage setImage:[UIImage imageNamed:@"icon_mima"]];
    [self.view addSubview:mimaImage];
    
    [mimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(yzmView.mas_top).with.offset(-10);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        
    }];
    
    self.possWordText = [[UITextField alloc] init];
    self.possWordText.placeholder =@"请输入登录密码";
    self.possWordText.font = [UIFont systemFontOfSize:14.f];
    //    self.possWordText.keyboardType=UIKeyboardTypeNumberPad;
    //    self.phoneText.secureTextEntry = YES;
    self.possWordText.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.possWordText.delegate = self;
    [self.view addSubview:self.possWordText];
    [self.possWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(mimaImage).with.offset(22);
        make.centerY.equalTo(mimaImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.registerButton];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(yzmView.mas_bottom).with.offset(50);
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).with.offset(-40);
        
        
    }];
    [self.registerButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.registerButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_c"] forState:UIControlStateNormal];
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

//登录
- (void)registerButtonAction:(UIButton *)sender {
    if (self.phoneText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"账号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.possWordText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"密码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.phoneText.text.length != 11) {
        self.hud=[WKProgressHUD popMessage:@"请输入11位账号" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.possWordText.text.length < 8 && self.possWordText.text.length > 15) {
        self.hud=[WKProgressHUD popMessage:@"请输入18~15个字符" inView:self.view duration:1 animated:YES];
        return;
    }
    
    NSDictionary * params = @{@"account" : self.phoneText.text,
                              @"password" : self.possWordText.text
                              };
    self.registerButton.userInteractionEnabled=NO;
    [GPosAPI requestOTCLoninWithParams:params andCallback:^(id obj) {
        NSLog(@"----obj--%@",obj);
        
        if ( obj == nil )
        {
            
            self.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            self.registerButton.userInteractionEnabled=YES;
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                
                NSDictionary *s1=[obj objectForKey:@"data"];
                [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"openId"] forKey:@"OTC_openId"];
                XMLightningViewController * VC = [[XMLightningViewController alloc] init];
                VC.hidesBottomBarWhenPushed = YES;
                //                 [self.navigationController pushViewController:VC animated:YES];
                //跳转时关闭当前页面
                NSArray *vcs = self.navigationController.viewControllers;
                NSMutableArray *newVCS = [NSMutableArray array];
                
                if ([vcs count] > 0) {
                    
                    for (int i=0; i < [vcs count]-1; i++) {
                        
                        [newVCS addObject:[vcs objectAtIndex:i]];
                        
                    }
                    
                }
                
                [newVCS addObject:VC];
                [self.navigationController setViewControllers:newVCS animated:YES];
                
                self.registerButton.userInteractionEnabled=YES;
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                self.registerButton.userInteractionEnabled=YES;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
                
                
            }else {
                self.registerButton.userInteractionEnabled=YES;
                self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                
            }
            
            
        }
        
        
    }];
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.phoneText.text.length >= 11) {
            self.phoneText.text = [textField.text substringToIndex:11];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.phoneText.isSecureTextEntry ==YES)
        {
            self.phoneText.text=allstr;
            return NO;
        }
    }
    
    //    if (textField == self.possWordText ) {
    //        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
    //        if (range.length == 1 && string.length == 0) {
    //            return YES;
    //        }
    //        else if (self.possWordText.text.length >= 15) {
    //            self.possWordText.text = [textField.text substringToIndex:15];
    //
    //            return NO;
    //        }
    //        //设置密文切换时保持之前的输入内容
    //        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
    //        if (self.possWordText.isSecureTextEntry ==YES)
    //        {
    //            self.possWordText.text=allstr;
    //            return NO;
    //        }
    //    }
    
    return YES;
}



@end
